package com.ts.api.module.app.org.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.context.ChannelUserContext;
import com.ts.api.module.app.org.entity.vo.RespAppMatchVO;
import com.ts.api.module.channel.entity.cache.ChannelCacheUser;
import com.ts.api.module.product.entity.po.ProductUserMatchPO;
import com.ts.api.module.product.service.ProductUserMatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  APP机构接口
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/app/org")
public class AppOrgController {

    @Autowired
    private ProductUserMatchService productUserMatchService;

    /**
     *  根据用户获取所有已经匹配的机构列表
     *
     *  @param  requestVO
     *  @return ResultVO
     */
    @GetMapping("/listByUser")
    public ResultVO<List<RespAppMatchVO>> listByUser(@RequestParamVo RequestVO requestVO) {
        ChannelCacheUser cacheUser = ChannelUserContext.get();

        List<ProductUserMatchPO> list = productUserMatchService.listByUser(cacheUser.getUserId());
        if (ValidParam.isEmpty(list)) {
            return ResultVO.fail("无记录信息");
        }

        List<RespAppMatchVO> listOrgVO = RespAppMatchVO.mappingListOrg(list);
        return ResultVO.suc(listOrgVO);
    }
}
